package com.ruoyi.network.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.network.common.validation.UpdateGroupValidation;
import com.ruoyi.network.model.vo.IpRangeCountVO;
import com.ruoyi.network.model.vo.IpRangePageVO;
import com.ruoyi.network.model.vo.IpRangeRequestVO;
import com.ruoyi.network.service.IIpRangeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.groups.Default;
import java.util.List;

/**
 * IP段管理 前端控制器
 *
 * @author lyp
 * @date 2024-12-05
 */
@RestController
@RequestMapping("/ip/range")
@Api(tags = "IP段管理")
public class IpRangeController extends BaseController {
    @Resource
    private IIpRangeService ipRangeService;

    /**
     * 查询IP段结构列表
     */
    @ApiOperation(value = "查询IP段结构列表", response = TableDataInfo.class)
    @GetMapping("/list")
    public AjaxResult list(IpRangePageVO vo) {
        return AjaxResult.success(ipRangeService.selectIpRangeList(vo));
    }

    /**
     * 导出IP段结构列表
     *
     * @param response 响应对象
     * @param vo       查询参数
     */
    @Log(title = "IP段结构", businessType = BusinessType.EXPORT)
    @ApiOperation(value = "导出IP段结构列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, IpRangePageVO vo) {
        try {
            ipRangeService.export(response, vo);
        } catch (Exception e) {
            throw new ServiceException("数据导出失败!");
        }
    }

    /**
     * 导入IP段数据
     *
     * @param file 待解析文件
     */
    @Log(title = "IP段结构", businessType = BusinessType.IMPORT)
    @ApiOperation(value = "导入IP段数据", response = TableDataInfo.class)
    @PostMapping("/import")
    public AjaxResult importExcel(@RequestParam MultipartFile file) {
        return toAjax(ipRangeService.importExcel(file));
    }

    /**
     * 获取IP段结构详细信息
     */
    @ApiOperation(value = "获取IP段结构详细信息", response = AjaxResult.class)
    @GetMapping(value = "/getId")
    public AjaxResult getInfo(Long id) {
        return success(ipRangeService.selectIpRangeById(id));
    }

    /**
     * 新增IP段结构
     */
    @Log(title = "IP段结构", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增IP段结构", response = AjaxResult.class)
    @PostMapping("/add")
    public AjaxResult add(@Validated({Default.class}) @RequestBody IpRangeRequestVO vo) {
        return toAjax(ipRangeService.insertIpRange(vo));
    }

    /**
     * 修改IP段结构
     */
    @Log(title = "IP段结构", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改IP段结构", response = AjaxResult.class)
    @PutMapping("/edit")
    public AjaxResult edit(@Validated({UpdateGroupValidation.class}) @RequestBody IpRangeRequestVO vo) {
        return toAjax(ipRangeService.updateIpRange(vo));
    }

    /**
     * 删除IP段结构
     */
    @Log(title = "IP段结构", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除IP段结构", response = AjaxResult.class)
    @DeleteMapping("/delete")
    public AjaxResult remove(@RequestParam(value = "ids") List<Long> ids) {
        return toAjax(ipRangeService.deleteIpRangeByIds(ids));
    }

    /**
     * 获取IP段统计
     */
    @ApiOperation(value = "获取IP段统计", response = AjaxResult.class)
    @GetMapping("/count")
    public AjaxResult count(IpRangeCountVO vo) {
        return success(ipRangeService.count(vo));
    }
}
